package com.ruoyi.produce.base.offer.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.produce.base.offer.domain.*;
import com.ruoyi.produce.base.offer.mapper.ProduceOfferMapper;
import com.ruoyi.psi.base.product.domain.ProductDto;
import com.ruoyi.psi.base.product.mapper.ProductMapper;
import com.ruoyi.utils.ID.IdUtil;
import com.ruoyi.utils.UtilsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 生产报价
 * 业务处理层
 */
@Service
public class ProduceOfferServiceImpl implements IProduceOfferService {
    @Autowired
    private ProduceOfferMapper produceOfferMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 根据搜索条件查询所有生产报价信息
     *
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<ProduceOfferDto> selectProduceOfferList(ProduceOfferCond cond) {
        List<ProduceOfferDto> produceOfferDtos = produceOfferMapper.selectProduceOfferList(cond);
        for (ProduceOfferDto produceOfferDto : produceOfferDtos) {
            // 通过产品编号，获取产品的响应数据
            ProductDto productDto = productMapper.selectProductById(produceOfferDto.getProduceId());
            // 类别名称
            produceOfferDto.setBaseCategoryName(productDto.getBaseCategoryName());
            double totalCost = 0.00;
            // 判断子表是否存在
            if (StringUtils.isNotNull(produceOfferMapper.selectProduceOfferListListById(produceOfferDto.getProduceOfferId()))) {
                List<ProduceOfferListDto> produceOfferListDtos = produceOfferMapper.selectProduceOfferListListById(produceOfferDto.getProduceOfferId());
                for (ProduceOfferListDto produceOfferListDto : produceOfferListDtos) {
                    // 计算子表单位成本
                    double costPrice = produceOfferListDto.getCostNumber() * produceOfferListDto.getCostUnitPrice();
                    produceOfferListDto.setCostPrice(costPrice);
                    // 累加总成本
                    totalCost += (costPrice * produceOfferDto.getOfferNumber());
                }
                // 查询子表
                produceOfferDto.setOfferListDtoList(produceOfferListDtos);
            }
            produceOfferDto.setTotalCost(totalCost);
        }
        return produceOfferDtos;
    }

    /**
     * 按编号查询具体的生产报价信息
     *
     * @param produceOfferId
     * @return
     */
    @Override
    public ProduceOfferDto selectProduceOfferById(String produceOfferId) {
        ProduceOfferDto produceOffer = produceOfferMapper.selectProduceOfferById(produceOfferId);
        // 通过产品编号，获取产品的响应数据
        ProductDto productDto = productMapper.selectProductById(produceOffer.getProduceId());
        // 类别名称
        produceOffer.setBaseCategoryName(productDto.getBaseCategoryName());
        double totalCost = 0.00;
        // 判断子表是否存在
        if (StringUtils.isNotNull(produceOfferMapper.selectProduceOfferListListById(produceOffer.getProduceOfferId()))) {
            List<ProduceOfferListDto> produceOfferListDtos = produceOfferMapper.selectProduceOfferListListById(produceOffer.getProduceOfferId());
            for (ProduceOfferListDto produceOfferListDto : produceOfferListDtos) {
                // 计算子表单位成本
                double costPrice = produceOfferListDto.getCostNumber() * produceOfferListDto.getCostUnitPrice();
                produceOfferListDto.setCostPrice(costPrice);
                // 累加总成本
                totalCost += (costPrice * produceOffer.getOfferNumber());
            }
            // 查询子表
            produceOffer.setOfferListDtoList(produceOfferListDtos);
        }
        produceOffer.setTotalCost(totalCost);
        return produceOffer;
    }

    /**
     * 新增生产报价记录
     *
     * @param produceOffer
     * @return
     */
    @Override
    public int insertProduceOffer(ProduceOfferDomain produceOffer) {
        if (StringUtils.isNotNull(produceOfferMapper.selectProduceOfferById(produceOffer.getProduceOfferId()))) {
            //存在
            return 0;
        }
        //不存在
        String produceOfferId = idUtil.createProduceOfferId();//自动生成编号
        produceOffer.setProduceOfferId(produceOfferId);//主表添加编号
        produceOfferMapper.insertProduceOffer(produceOffer);
        // 添加明细信息
        List<ProduceOfferListDomain> offerListDomainList = produceOffer.getOfferListDomainList();
        System.out.println("新增" + offerListDomainList);
        for (int i = 0; i < offerListDomainList.size(); i++) {
            // 给子表设置编号
            offerListDomainList.get(i).setProduceOfferId(produceOfferId);
            produceOfferMapper.insertProduceOfferList(offerListDomainList.get(i));
        }
        return 1;
    }

    /**
     * 修改生产报价记录
     *
     * @param produceOffer
     * @return
     */
    @Override
    public int updateProduceOffer(ProduceOfferDomain produceOffer) {
        if (!StringUtils.isNotNull(produceOfferMapper.selectProduceOfferById(produceOffer.getProduceOfferId()))) {
            //不存在
            return 0;
        }
        //存在
        produceOfferMapper.updateProduceOffer(produceOffer);
        // 从数据库中查询已有的数据
        List<ProduceOfferListDto> produceOfferListDtos = produceOfferMapper.selectProduceOfferListListById(produceOffer.getProduceOfferId());
//        System.out.println("数据库中查询的数据" + produceOfferListDtos);
        // 查询前端传入的修改数据
        List<ProduceOfferListDomain> offerListDomainList = produceOffer.getOfferListDomainList();
//        System.out.println("查询前端传入的修改数据" + offerListDomainList);
        // 遍历前端传入的子表数据
        for (int i = 0; i < offerListDomainList.size(); i++) {
            // 获取指定的明细
            ProduceOfferListDto produceOfferListDto = produceOfferMapper.selectProduceOfferListById(produceOffer.getProduceOfferId(), offerListDomainList.get(i).getCostItemId());
//            System.out.println("具体的修改对象" + (i + 1) + produceOfferListDto);
            if (StringUtils.isNotNull(produceOfferListDto)) {
                // 存在指定的明细
                for (int j = 0; j < produceOfferListDtos.size(); j++) {
                    if (produceOfferListDtos.get(j).getCostItemId().equals(produceOfferListDto.getCostItemId())) {
                        produceOfferListDtos.remove(j);
                        break;
                    }
                }
//                System.out.println("数据库之后的数据" + (i + 1) + produceOfferListDtos);
                produceOfferMapper.updateProduceOfferList(offerListDomainList.get(i)); //更新
//                System.out.println("修改之后的数据" + (i + 1) + produceOfferMapper.updateProduceOfferList(offerListDomainList.get(i)));
            } else {
                produceOfferMapper.insertProduceOfferList(offerListDomainList.get(i)); //新增
            }
        }
        for (int i = 0; i < produceOfferListDtos.size(); i++) {
            //删除新旧重叠以外的部分
            produceOfferMapper.deleteProduceOfferListById(produceOfferListDtos.get(i).getProduceOfferId(), produceOfferListDtos.get(i).getCostItemId());
        }
        return 1;
    }

    /**
     * 删除生产报价记录
     * 仅修改删除标志
     *
     * @param produceOffer
     * @return
     */
    @Override
    public int updateProduceOfferDeleteFlag(ProduceOfferDomain produceOffer) {
        return produceOfferMapper.updateProduceOfferDeleteFlag(produceOffer);
    }

    /**
     * 生产报价详细查询
     *
     * @param cond
     * @return
     */
    @Override
    public List<ProduceOfferListDto> selectProduceOfferListList(ProduceOfferCond cond) {
        return produceOfferMapper.selectProduceOfferListList(cond);
    }

    /**
     * 查询生产报价对应的所有报价详细
     *
     * @param produceOfferId 生产报价编号
     * @return
     */
    @Override
    public List<ProduceOfferListDto> selectProduceOfferListListById(String produceOfferId) {
        return produceOfferMapper.selectProduceOfferListListById(produceOfferId);
    }

    /**
     * 查询生产报价对应的指定报价详细
     * @param produceOfferId 生产报价编号
     * @param costItemId 成本项目编号
     * @return
     */
    @Override
    public ProduceOfferListDto selectProduceOfferListById(String produceOfferId, Integer costItemId) {
        return produceOfferMapper.selectProduceOfferListById(produceOfferId, costItemId);
    }

    /**
     * 根据生产计划编号查询是否有报价记录
     * @param planSelfId
     * @return
     */
    @Override
    public boolean selectProduceOfferByPlanSelfId(String planSelfId) {
        return StringUtils.isNotNull(produceOfferMapper.selectProduceOfferByPlanSelfId(planSelfId));
    }
}
